// components/society/s-group/s-group.js
import api from '../../../utils/api.js'
let app = getApp();
import { HTTP } from '../../../utils/http.js'
let http = new HTTP()
import { config } from '../../../utils/config'
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },
    data: {
        currentTab: 0,
        info1: {}, //创建的团
        info2: {}, //加入的团
        memberList1: [],
        memberList2: [],
        group_id: null, // 子组件 切换 团队时，改变当前团id
        dynamicList: [],
        otherDynamicList1: [],
        otherDynamicList2: [], // 其他比邻团 3条 -点赞最高的动态 -排除自己所在的团
        myOrganizationInfo: 0,
        imgUrl: config.imgUrl,
    },
    ready: function() {


    },
    lifetimes: {
        attached: function() {
            // 在组件实例进入页面节点树时执行
            console.log('在组件实例进入页面节点树时执行');
            let user_id = wx.getStorageSync('user_id')
            this.getMyOrganizationInfo(user_id)
            this.getJoinMyOrganizationInfo(user_id)


        },
        detached: function() {
            // 在组件实例被从页面节点树移除时执行
        },
    },
    pageLifetimes: {
        show: function() {
            // 页面被展示
            console.log('父组件 展示出来了');
            let user_id = wx.getStorageSync('user_id')
            this.getMyOrganizationInfo(user_id)
            this.getJoinMyOrganizationInfo(user_id)
        },
    },
    methods: {
        //用户退出加入的团
        userExitOrganization(e) {
            let tid = e.currentTarget.dataset.tid;
            console.log('tid', tid)
            wx.showModal({
                title: '您确定要退团吗?',
                content: '退团后，不能再次进入这个团了！',
                success(res) {
                    if (res.confirm) {

                        http.request({
                            url: api.userExitOrganization,
                            data: {
                                id: wx.getStorageSync('user_id'), //用户id
                                tid: tid //团ID
                            },
                            success: (res) => {
                                if (res.code == 200) {
                                    console.log('用户退出成功！');
                                    //页面刷新或清空数据
                                    this.setData({
                                        info2: {}
                                    })
                                }

                            }
                        });
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })

        },
        // 申请团长
        goApplyHeader() {
            wx.navigateTo({
                url: "/pages/member-applyHeader/member-applyHeader",
            })
        },
        //拿到团在本市的排名
        getPaiming(tid, num) {
            http.request({
                url: api.getPaiming,
                data: {
                    id: wx.getStorageSync('city').id, //城市id
                    tid: tid //团ID
                },
                success: (res) => {
                    console.log('拿到团在本市的排名', res);
                    if (res.code == 200) {
                        let rank = 'rank' + num
                        console.log('rank', rank)
                        this.setData({
                            [rank]: res.data
                        })
                    }
                }
            });
        },

        //拿到用户的创建的团——详细信息
        getMyOrganizationInfo(user_id) {
            console.log('sssssssssss', "pages/index/index", )
            http.request({
                url: api.getMyOrganizationInfo,
                data: {
                    id: user_id,
                },
                success: (res) => {
                    console.log('拿到用户的创建团详细信息', res);
                    if (res.code == 200) {
                        let organization_id = res.data.id
                        this.getMyOrganizationUserList1(organization_id, user_id);
                        this.getOrganizationDynamicList1(organization_id, user_id);
                        this.getPaiming(organization_id, 1);
                        this.setData({
                            info1: res.data,
                            group_id: res.data.id,
                            height: 460,
                            swiper_height1: 460,
                            myOrganizationInfo: res.data.id
                        })

                    } else if (res.code == 40001) {
                        console.log('40001')
                        this.setData({
                            msg1: "您还没有创建比邻团",
                            info1: {},
                            swiper_height1: 460,
                            height: 460
                        })
                    }
                }
            });
        },
        //拿到用户加入的团信息
        getJoinMyOrganizationInfo(user_id) {
            http.request({
                url: api.getJoinMyOrganizationInfo,
                data: {
                    id: user_id,
                },
                success: (res) => {
                    console.log('拿到用户加入的团信息', res);
                    if (res.code == 200) {
                        let organization_id = res.data.id
                        this.getMyOrganizationUserList2(organization_id, user_id);
                        this.getOrganizationDynamicList2(organization_id, user_id)
                        this.getPaiming(organization_id, 2);
                        this.setData({
                            info2: res.data,
                            height2: 460,
                            swiper_height2: 460,
                        })

                    } else if (res.code == 201) {
                        console.log('201')
                        this.setData({
                            msg2: "您还没有加入比邻团",
                            info2: {},
                            swiper_height2: 160
                        })
                    }
                }
            });
        },
        //我创建的比邻团————友邻吧
        getOrganizationDynamicList1(id, user_id) {
            http.request({
                getOrganizationDynamicList: api.getOrganizationDynamicList,
                data: {
                    organization_id: id,
                    page: 1,
                    size: 3
                },
                success: (res) => {
                    console.log('拿到团的帖子————友邻吧', res);
                    if (res.code == 200) {
                        res.data.map((ele, index, arr) => {
                            this.sureOrganizationAttention1(ele.id, user_id, arr)
                        })
                        let line = res.data.length;
                        if (line > 3) {
                            line = 3
                        }
                        this.setData({
                            otherDynamicList1: res.data,
                            height: 460 + 300 * line,
                            swiper_height1: 460 + 300 * line,
                        })

                        console.log('line', line)

                    } else if (res.error_code == 201) {
                        console.log("该比邻团还没有动态，快去发布一个吧!")
                    }

                }
            });
        },
        //我加入的比邻团————友邻吧
        getOrganizationDynamicList2(id, user_id) {
            http.request({
                url: api.getOrganizationDynamicList,
                data: {
                    organization_id: id,
                    page: 1,
                    size: 3
                },
                success: (res) => {
                    console.log('拿到团的帖子————友邻吧', res);
                    if (res.code == 200) {
                        res.data.map((ele, index, arr) => {
                            this.sureOrganizationAttention2(ele.id, user_id, arr)
                        })
                        let line = res.data.length;
                        if (line > 3) {
                            line = 3
                        }
                        this.setData({
                            otherDynamicList2: res.data,
                            swiper_height2: 460 + 300 * line,
                        })

                        console.log('line', line)
                    } else if (res.error_code == 201) {
                        console.log("该比邻团还没有动态，快去发布一个吧!")
                    }

                }
            });
        },
        //拿到创建的团的用户列表
        getMyOrganizationUserList1(id) {
            http.request({
                url: "/Organization/getMyOrganizationUserList",
                data: {
                    id: id,
                },
                success: (res) => {
                    console.log('拿到团的用户列表1', res);
                    if (res.code == 200) {
                        this.setData({
                            memberList1: res.data,
                        })
                    }

                }
            });
        },
        //拿到加入的团的用户列表
        getMyOrganizationUserList2(id) {
            http.request({
                url: "/Organization/getMyOrganizationUserList",
                data: {
                    id: id,
                },
                success: (res) => {
                    console.log('拿到团的用户列表2', res);
                    if (res.code == 200) {
                        this.setData({
                            memberList2: res.data
                        })
                    }

                }
            });
        },
        //当用户没有加入一个比邻团时，去加入
        goUserJoinO() {
            wx.navigateTo({
                url: '/pages/member-userJoinO/member-userJoinO',
            })
        },

        // 滑动切换
        swiperTab: function(e) {
            let group1 = this.data.info1.id;
            let group2 = this.data.info2.id;
            console.log('group1', group1)
            console.log('group2', group2)


            let num = e.detail.current
            if (num == 0) {
                let height = this.data.swiper_height1
                this.setData({
                    height,
                    group_id: group1
                });
                // let myEventDetail = { id: group1} 
                // this.triggerEvent('myevent', myEventDetail)
            } else if (num == 1) {
                let height = this.data.swiper_height2
                this.setData({
                    height,
                    group_id: group2
                });
                // let myEventDetail = { id: group2 }
                // this.triggerEvent('myevent', myEventDetail)
            }

            this.setData({
                currentTab: e.detail.current
            });

        },
        //点击切换
        clickTab: function(e) {
            let group1 = this.data.info1.id;
            let group2 = this.data.info2.id;
            console.log(this.data.info1)
            console.log('group1', group1)
            console.log('group2', group2)


            let num = e.target.dataset.current;
            if (num == 0) {
                let height = this.data.swiper_height1
                this.setData({
                    height,
                    group_id: group1
                });

                // let myEventDetail = { id: group1 }
                // this.triggerEvent('myevent', myEventDetail)

            } else if (num == 1) {
                let height = this.data.swiper_height2
                this.setData({
                    height,
                    group_id: group2
                });

                // let myEventDetail = { id: group2 }
                // this.triggerEvent('myevent', myEventDetail)
            }

            if (this.data.currentTab === e.target.dataset.current) {
                return false;
            } else {
                this.setData({
                    currentTab: e.target.dataset.current
                })
            }
        },
        // 去往-比邻团团员 页面
        goMemberPage(e) {
            console.log(e)
            let id = e.currentTarget.dataset.organization_id; //当前比邻团id
            wx.navigateTo({
                url: `/pages/member-member/member-member?id=${id}`,
            })
        },
        //比邻团动态页面
        goDynamicPage(e) {
            let id = e.currentTarget.dataset.organization_id;
            let community_name = e.currentTarget.dataset.community_name;
            let groupInfo = {
                id: id,
                community_name: community_name
            }
            wx.setStorageSync('groupInfo', groupInfo)
            wx.navigateTo({
                url: `/pages/member-dynamic/member-dynamic?id=${id}&community_name=${community_name}`,
            })
        },
        //进入邀请邻居
        goInviteNeighborPage(e) {
            console.log('eeeeeeeee', e);
            let group_id = this.data.group_id;
            //用户 既有创建团 也有 加入团时 ，即info1 ,info2都存在
            let info1 = this.data.info1;
            let info2 = this.data.info2;
            let arr = [];
            arr.push(info1)
            arr.push(info2)
            arr.forEach((ele) => {
                if (ele.id == group_id) {
                    wx.setStorageSync('inviteOrganizationInfo', ele)
                }
            })
            wx.navigateTo({
                url: `/pages/member-inviteNeighbor/member-inviteNeighbor?group_id=${group_id}`,
            })
        },


        //判断用户是否已点赞一条帖子
        sureOrganizationAttention1(id, uid, arr) {
            http.request({
                url: "/Organization/sureOrganizationAttention",
                data: {
                    id: id, //帖子ID
                    uid: uid, //用户id
                },
                success: (res) => {
                    console.log('判断用户是否已点赞一条帖子', res);
                    if (res.code == 200) {
                        arr.forEach((ele) => {
                            if (ele.id == id) {
                                ele.like = true;
                            }
                        })
                        this.setData({
                            otherDynamicList1: arr
                        })
                    } else if (res.code == 201) {
                        arr.forEach((ele) => {
                            if (ele.id == id) {
                                ele.like = false;
                            }
                        })
                        this.setData({
                            otherDynamicList1: arr
                        })
                    }
                }
            });
        },
        //用户点赞或者取消点赞一条帖子
        //statusID————1点赞  2取消点赞
        addOrganizationAttention1(e) {
            let otherDynamicList1 = this.data.otherDynamicList1;
            let id = e.currentTarget.dataset.id;
            let uid = wx.getStorageSync('user_id');
            let statusID = e.currentTarget.dataset.statusid;
            http.request({
                url: "/Organization/addOrganizationAttention",
                data: {
                    id, //帖子ID
                    uid, //用户id
                    statusID
                },
                success: (res) => {
                    if (res.code == 200 && statusID == 1) {
                        console.log('用户点赞成功', res);
                        otherDynamicList1.forEach((ele) => {
                            if (ele.id === id) {
                                ele.like = true
                                ele.attention += 1
                            }
                        })
                        this.setData({
                            otherDynamicList1
                        })
                    } else if (res.code == 200 && statusID == 2) {
                        console.log('用户取消点赞点赞成功', res);
                        otherDynamicList1.forEach((ele) => {
                            if (ele.id === id) {
                                ele.like = false
                                ele.attention -= 1
                            }
                        })
                        this.setData({
                            otherDynamicList1
                        })
                    }
                }
            });
        },



        //判断用户是否已点赞一条帖子
        sureOrganizationAttention2(id, uid, arr) {
            http.request({
                url: "/Organization/sureOrganizationAttention",
                data: {
                    id: id, //帖子ID
                    uid: uid, //用户id
                },
                success: (res) => {
                    console.log('判断用户是否已点赞一条帖子', res);
                    if (res.code == 200) {
                        arr.forEach((ele) => {
                            if (ele.id == id) {
                                ele.like = true;
                            }
                        })
                        this.setData({
                            otherDynamicList2: arr
                        })
                    } else if (res.code == 201) {
                        arr.forEach((ele) => {
                            if (ele.id == id) {
                                ele.like = false;
                            }
                        })
                        this.setData({
                            otherDynamicList2: arr
                        })
                    }
                }
            });
        },
        //用户点赞或者取消点赞一条帖子
        //statusID————1点赞  2取消点赞
        addOrganizationAttention2(e) {
            let otherDynamicList2 = this.data.otherDynamicList2;
            let id = e.currentTarget.dataset.id;
            let uid = wx.getStorageSync('user_id');
            let statusID = e.currentTarget.dataset.statusid;
            http.request({
                url: "/Organization/addOrganizationAttention",
                data: {
                    id, //帖子ID
                    uid, //用户id
                    statusID
                },
                success: (res) => {
                    if (res.code == 200 && statusID == 1) {
                        console.log('用户点赞成功', res);
                        otherDynamicList2.forEach((ele) => {
                            if (ele.id === id) {
                                ele.like = true
                                ele.attention += 1
                            }
                        })
                        this.setData({
                            otherDynamicList2
                        })
                    } else if (res.code == 200 && statusID == 2) {
                        console.log('用户取消点赞点赞成功', res);
                        otherDynamicList2.forEach((ele) => {
                            if (ele.id === id) {
                                ele.like = false
                                ele.attention -= 1
                            }
                        })
                        this.setData({
                            otherDynamicList2
                        })
                    }
                }
            });
        },


    },


})